package com.huayue.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huayue.dao.BaseDao;
import com.huayue.dao.HousekeepOrderDao;
import com.huayue.dao.HousekeepOrderDetailsDao;
import com.huayue.entity.HousekeepOrder;
import com.huayue.entity.HousekeepOrderDetails;

/**
 * 房务订单
 */
@Service
public class HousekeepOrderService extends BaseService<HousekeepOrder> {
    @Autowired
    HousekeepOrderDao dao;
    @Autowired
    HousekeepOrderDetailsDao detailsDao;

    @Override
    public BaseDao<HousekeepOrder> getDao() {
        return dao;
    }

    /**
     * 查找最大id
     *
     * @return
     */
    public int findMaxId() {

        int i = 0;
        try {
            i = dao.findMaxId();
        } catch (Exception e) {
            i = 0;
        }
        return i;
    }

    public int checkOut(HousekeepOrder housekeepOrder) {
        return dao.checkOut(housekeepOrder);
    }

    public List<Map<String, Object>> findDetails(String id) {
        return detailsDao.findDetails(id);
    }

    /**
     * 添加订单信息
     *
     * @param orderInfo
     * @param details
     * @return
     */
    public boolean addDetails(HousekeepOrder orderInfo, List<HousekeepOrderDetails> details) {
        // 成功数量
        int success = 0;
        // 完善订单信息
        // 将订单信息添加到数据库
        success = dao.insert(orderInfo);
        // 判断是否添加成功
        if (success == 1) {
            // 将订单详情添加到数据库
            for (HousekeepOrderDetails order : details) {
                success += detailsDao.insert(order);
            }
        }
        if (success == (details.size() + 1)) {
            return true;
        }
        return false;
    }

    /**
     * 查询当前库存布草套数
     * 
     * @return
     */
    public int minDepotNumber() {
        return dao.minDepotNumber();
    }

    public int check(HousekeepOrder housekeepOrder, Integer number) {

        dao.changeDepotNumber(number);
        // 房间恢复为空房并更改订单状态
        return dao.changeStatus(housekeepOrder, number);
    }
}
